﻿using System.Windows.Controls;
using System.Windows.Input;
using LFSRecord2.Model;
using LFSRecord2.ViewModel;

namespace LFSRecord2.View
{
    /// <summary>
    /// Interaction logic for MediaView.xaml
    /// </summary>
    public partial class MediaView : UserControl
    {
        public MediaView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            setupContextMenu();
        }

        private void MediaGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Debug.WriteLine("KKKKKKKKKKKKKKLick");
            e.Handled = true;
            LfsRecordController.project.DeselectAllMedia();
        }

        void setupContextMenu()
        {
            MenuItem mi, mi2;
            ContextMenu cm = new ContextMenu();

            mi = new MenuItem();
            mi.Name = "Import";
            mi.Header = "Import";

            mi2 = new MenuItem();
            mi2.Name = "ImportMediaFile";
            mi2.Header = "Import Media File";
            mi2.Command = ImportMediaCommand;
            mi.Items.Add(mi2);

            mi2 = new MenuItem();
            mi2.Name = "ImportRDLfs";
            mi2.Header = "Replay Data From LFS";
            mi2.Command = ImportRDFromLFSCommand;
            mi.Items.Add(mi2);

            cm.Items.Add(mi);
            ContextMenu = cm;
        }

        // ImportMediaFileCommand
        RelayCommand _importMediaCommand;
        ICommand ImportMediaCommand
        {
            get
            {
                if (_importMediaCommand == null)
                    _importMediaCommand = new RelayCommand(param => ImportMediaCommandExecute());
                return _importMediaCommand;
            }
        }
        static void ImportMediaCommandExecute()
        {
            MainWindowViewModel.ShowImportMediaDialog();
        }

        // ImportRDFromLFSCommand
        RelayCommand _importRDFromLFSCommand;
        ICommand ImportRDFromLFSCommand
        {
            get
            {
                if (_importRDFromLFSCommand == null)
                    _importRDFromLFSCommand = new RelayCommand(param => ImportRDFromLFSCommandExecute(), param => ImportRDFromLFSCommandCanExecute());
                return _importRDFromLFSCommand;
            }
        }
        static void ImportRDFromLFSCommandExecute()
        {
            MainWindowViewModel.ShowImportReplayDataFromLFSDialog();
        }
        static bool ImportRDFromLFSCommandCanExecute()
        {
            return (LfsRecordController.project.HasReplay() && LfsRecordController.insim != null && LfsRecordController.insim.IsConnected);
        }
    }
}
